
//搜索用的数据
var DATA = {

    customer_id : '',

    page : 1,

    per_page : 20,

    remark : '',

    advert_id : '',

    peacock_id : '',

    start_at: '',

    end_at: ''

};

//vue的data
var _Global_Data = {

    query_info : DATA,      //查询所需要的数据

    advert_logs : {},       //所有播放记录

    advert_logs_data :[],   //记录列表

    search_info : {},       //用于接受查询数据

    subordinates : {},      //当前权限的下级

    machine_lists : [],      //存在的机器

    advert_detail:{},    //当前广告详情

    is_buy_purchase : false,    //是否购买了广告服务

    selected_advert_type:1,      //当前所选的广告类型

    advert_type_str_map : { // advert type  => string map
        "1" :'图片',
        "2" :'视频',
        "3" :'文字'
    },

    area_type_str_map : { // area type  => string map
        "1" :'全国',
        "2" :'区域',
        "3" :'机器'
    }
};

var AdvertLogResource = new Resource( API_LIST.ADVERT_PLAY_LOG , Vue.http );

!function advertLogInfo() {

    var vm = new Vue({

        el : '#advert_log_table',

        data : _Global_Data,

        created : function(){

            // this.initSearchData()
            var self = this;

            self.initSearchData()
                .then(self.requestTableData(self.query_info))

        },

        computed : {
            currentAdvertSize : function currentAdvertSize() {
                if(this.advert_detail.ratio_numerator == this.advert_detail.ratio_denominator ){
                    return '全屏'
                }else {
                    return this.advert_detail.ratio_numerator + '/'+ this.advert_detail.ratio_denominator;
                }

            },
            currentAdvertAreaType : function currentAdvertAreaType() {
                if(this.advert_detail.area_type == 1){
                    return '全国';
                }
                if(this.advert_detail.area_type == 2){
                    return '区域';
                }
                if(this.advert_detail.area_type == 3){
                    return '机器';
                }
            },
            currentAdvertType : function currentAdvertType() {
                if(this.advert_detail.advert_type == 1){
                    return '图片';
                }
                if(this.advert_detail.advert_type == 2){
                    return '视频';
                }
                if(this.advert_detail.advert_type == 3){
                    return '文字';
                }
            },
            currentAdvertPlaySecond : function currentAdvertPlaySecond() {
                return this.advert_detail.play_second / 60 + '分钟';
            }
        },

        methods : {
            //select2获取value的方法
            getValue : function getValue(e){

                this.query_info[e.target.dataset.name] = e.target.value;

            },
            //清空日期的方法
            resetTime : function resetTime( id ){

                $(id).val("");

            },
            //点击搜索时触发的函数
            confirmSearch : function confirmSearch(){

                this.query_info.start_at = $('.start-time').val();

                this.query_info.end_at = $('.end-time').val();

                var json_object = JSON.stringify(this.query_info);

                this.search_info = JSON.parse(json_object);

                this.requestTableData(this.query_info);

            },

            //初始化搜索框所需数据
            initSearchData : function initSearchData(){

                var self = this;

                return AdvertLogResource.create()
                                        .then(function(res){

                                            if( parseInt(res.data.code) === 0 ){

                                                self.subordinates = res.data.subordinates;

                                                self.machine_lists = res.data.peacocks;

                                            }else {

                                                alert(res.data.error);

                                            }

                                        })
                                        .catch(function(err){

                                            console.log(err);

                                            alert('请求异常，请重试！')

                                        })
            },
            //请求表格数据函数
            requestTableData : function requestTableData(params){

                var self = this;

                App.blockUI("#query_form");

                AdvertLogResource.index( params )
                                .then(function(res){
                                    if( parseInt(res.data.code) === 0 ){

                                        self.advert_logs = res.data.advert_logs;

                                        self.advert_logs_data = res.data.advert_logs.data;


                                    }else {

                                        alert(res.data.error);

                                    }

                                    App.unblockUI("#query_form");

                                })
                                .catch(function(err){

                                    console.log(err);

                                    App.unblockUI("#query_form");

                                    alert('初始化数据失败，请刷新重试！')

                                })

            },
            queryAdvert :function queryAdvert(advert_detail) {

                this.advert_detail = advert_detail;
            },
            //改变页数的时候出发的函数
            pageChange : function pageChange(val){

                this.search_info.page = val;

                this.search_info.per_page = this.query_info.per_page;

                this.requestTableData(this.search_info);

            },
            //改变每页显示数时触发的函数
            sizeChange : function sizeChange(val){

                this.search_info.per_page = val;

                this.query_info.per_page = val;

                this.requestTableData(this.search_info);

            }

        }

    })

}();